<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>商品列表</span>
    </div>
    <div class="content">
      <el-table :data="data" style="width: 80%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品ID" >
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="所属分类">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="商品价格">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="商品图片">
                <img
                  :src="API + props.row.imgUrl"
                  width="80"
                  height="80"
                  alt=""
                />
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.ctime }}</span>
              </el-form-item>
              <el-form-item label="商品评价">
                <span>{{ props.row.rating }}</span>
              </el-form-item>
              <el-form-item label="商品销量">
                <span>{{ props.row.sellCount }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ props.row.goodsDesc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" width="180">
        </el-table-column>
        <el-table-column prop="category" label="所属分类" width="180">
        </el-table-column>
        <el-table-column prop="price" label="商品价格"> </el-table-column>
        <el-table-column label="商品图片">
          <template slot-scope="scope">
            <div>
              <img
                :src="API + scope.row.imgUrl"
                width="80"
                height="80"
                alt=""
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="goodsDesc" label="商品描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除此账号吗？"
              @confirm="handleDelete(scope.row)"
              @cancel="canceldel"
            >
              <el-button
                slot="reference"
                size="mini"
                type="danger"

                >删除</el-button
              ></el-popconfirm
            >
          </template>
        </el-table-column>
      </el-table>

<el-dialog title="商品信息" :visible.sync="dialogFormVisible">
  <el-form :model="editdata">
    <el-form-item label="商品名称" >
      <el-input v-model="editdata.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="商品分类" >
      <el-input v-model="editdata.category" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" >
      <el-input v-model="editdata.price" autocomplete="off"></el-input>
    </el-form-item>
     <el-form-item label="商品描述" >
      <el-input v-model="editdata.goodsDesc" autocomplete="off"></el-input>
    </el-form-item>
    <span>商品图片</span> <br>  <br>
     
  </el-form>

 

  <div slot="footer" class="dialog-footer">
    <el-button @click="canceledit">取 消</el-button>
    <el-button type="primary" @click="ackedit(editdata)">确 定</el-button>
  </div>
</el-dialog>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 8, 5, 3]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
      >
      </el-pagination>
   
    </div>
  </el-card>
</template>

<script>
import { getGoodslist, delGood, goodsmodify } from "@/api/goods";
export default {
  name: "GoodsList",
  data() {
    return {
      date:[{id:1,value:'3322',date:'2022-01-23'}],
      dialogVisible: false,
      data: [],
      API: "http://127.0.0.1:5000",
      currentPage: 1,
      pageSize: 6,
      editdata:{},
       dialogFormVisible: false,
    };
  },
  methods: {
   async ackedit(b){
    console.log(b);
     let data = {...b}
     data.imgUrl = data.imgUrl.substr(46)
      this.dialogFormVisible = false
      let res = await  goodsmodify(data)
      console.log(res);
      


    },
    canceledit(){

      this.dialogFormVisible = false
      this.$message({
        type:'info',
        message:"取消成功"
      })

    },
   async handleDelete(b){

   let id =b.id
   
   
   
let res = await delGood({id})
if(res.data.code==0){
    this.getData();

}
console.log(res,888);

    },
    async getData() {
      let res = await getGoodslist({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      console.log(res);
      this.data = res.data.data;
      console.log(this.data,555);
      
    },
    handleEdit(a) {
      this.editdata=a
      this.dialogFormVisible = true
     console.log(a,55);
     
    },
 
    canceldel() {
      this.$message({
        message: "取消成功",
        type: "success",
      });
    },
    saveeditinfo() {
      console.log(this.data);
      
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.el-form-item {
  width: 48%;
}
.el-button--danger {
  margin-left: 10px;
}
</style>
